﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Linq;
using A2DFramework.EventService.Configuration;

namespace A2DFramework.EventService.LocalEventBus
{
    public class LocalEventBusProvider : IEventBusProvider
    {
        public void Emit(IEventBase evt)
        {
            var result = from tb in EventServiceConfiguration.EventMapping
                         where tb.Key.Equals(evt.GetType())
                         select tb;

            foreach (KeyValuePair<Type, List<Type>> t in result.ToList())
            {
                foreach (Type tt in t.Value)
                {
                    Type targetType = tt;
                    MethodInfo mi = targetType.GetMethod("Handle");
                    if (mi == null)
                        continue;

                    object o = Activator.CreateInstance(targetType);
                    mi.Invoke(o, new object[] { evt });
                }
            }
        }

    }
}
